//
//  MyTableViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/4.
//  Copyright © 2018年 longlong. All rights reserved.
//
#define CellIdentifer @"UITableViewCell"
#define TableHeaderHeight (ScreenWidth *  0.656)

#import "MyTableViewController.h"
#import <Masonry.h>
#import "MainTabbarController.h"
#import "FileViewController.h"
#import "LibraryViewController.h"
#import "NFCViewController.h"


@interface MyTableViewController ()
<UIScrollViewDelegate>

@property (nonatomic ,strong) UIView *tableHeaderView;
@property (nonatomic ,strong) UIButton *logoutButton;

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    self.tableView.tableHeaderView = self.tableHeaderView;
    self.tableView.tableFooterView = self.logoutButton;

    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifer];
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    
    if (indexPath.row == 0)
    {
        cell.textLabel.text = @"上传";
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = @"下载";
    }
    else if (indexPath.row == 2)
    {
        cell.textLabel.text = @"文件列表";
    }
    else if (indexPath.row == 3)
    {
        cell.textLabel.text = @"我的相册";
    }
    else if (indexPath.row == 4)
    {
        cell.textLabel.text = @"我的卡包";
    }
    else if (indexPath.row == 5)
    {
        cell.textLabel.text = @"我的NFC";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        
    }
    else if (indexPath.row == 2)
    {
        FileViewController *fileVC = [[FileViewController alloc] init];
        fileVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:fileVC animated:YES];
    }
    else if (indexPath.row == 3)
    {
        LibraryViewController *fileVC = [[LibraryViewController alloc] init];
        fileVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:fileVC animated:YES];
    }
    else if (indexPath.row == 4)
    {

    }
    else if (indexPath.row == 5)
    {
        NFCViewController *nfcVC = [[NFCViewController alloc] init];
        nfcVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:nfcVC animated:YES];
    }

    

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)logoutButtonClick
{
    [[AccountManager shareAccountManager] logoutAccount];
    [AuthorizationManager accessPermission];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset  = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + TableHeaderHeight ) / 2;
    UIImageView *backImage = [self.tableHeaderView viewWithTag:100];
    
    if (yOffset < 0)
    {
        CGRect rect = backImage.frame;
        rect.origin.y = yOffset;
        rect.size.height = TableHeaderHeight - yOffset ;
//        rect.origin.x = xOffset;
//        rect.size.width = ScreenWidth + fabs(xOffset)*2;
        backImage.frame = rect;
    }
}

#pragma mark - setter and getetr

- (UIView *)tableHeaderView
{
    if (_tableHeaderView == nil)
    {
        UIView *tableHeaderView= [[UIView alloc]initWithFrame:CGRectMake(0, - TableHeaderHeight, ScreenWidth,  TableHeaderHeight)];
        tableHeaderView.backgroundColor = [UIColor clearColor];
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"my_Back"]];
        imageView.frame = tableHeaderView.bounds;
        imageView.tag = 100;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [tableHeaderView addSubview:imageView];
        
        UIImageView *headerImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"register_Default"]];
        headerImageView.layer.cornerRadius = 35;
        headerImageView.clipsToBounds = YES;
        headerImageView.frame = tableHeaderView.bounds;
        headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        [tableHeaderView addSubview:headerImageView];
        headerImageView.frame = CGRectMake(0, 0, 70, 70);
        headerImageView.center = CGPointMake(ScreenWidth / 2.0, TableHeaderHeight / 2.0 - 20);
        
        
        UILabel *nickLable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headerImageView.frame), ScreenWidth, 50)];
        nickLable.textAlignment = NSTextAlignmentCenter;
        nickLable.textColor = [UIColor whiteColor];
        [tableHeaderView addSubview:nickLable];

        
        
        
        
        _tableHeaderView = tableHeaderView;
    }
    
    return _tableHeaderView;
}

- (UIButton *)logoutButton
{
    if (_logoutButton == nil)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"退出登录" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(logoutButtonClick) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, 0, ScreenWidth, 50);
        _logoutButton = button;
    }
    return _logoutButton;
}

@end
